# 应用启动脚本 - 后端和前端

Write-Host "======================================" -ForegroundColor Green
Write-Host "🚀 启动 CMS Low-Code Platform" -ForegroundColor Green  
Write-Host "======================================" -ForegroundColor Green

$rootPath = "d:\2025\JGSY.PP\Project\JGSY.CMS.LowCode.Platform"
$frontendPath = "$rootPath\wwwroot\frontend"

# 1. 启动后端
Write-Host "🔧 启动后端服务..." -ForegroundColor Blue
Set-Location $rootPath

Write-Host "📦 检查后端编译..." -ForegroundColor Yellow
dotnet build --verbosity quiet
if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ 后端编译失败" -ForegroundColor Red
    dotnet build
    exit 1
}
Write-Host "✅ 后端编译成功" -ForegroundColor Green

# 后台启动后端
Write-Host "🚀 启动后端服务器 (后台运行)..." -ForegroundColor Blue
$backendJob = Start-Job -ScriptBlock {
    Set-Location "d:\2025\JGSY.PP\Project\JGSY.CMS.LowCode.Platform"
    dotnet run --urls "http://localhost:5153"
}

# 等待后端启动
Write-Host "⏳ 等待后端启动..." -ForegroundColor Yellow
Start-Sleep -Seconds 10

# 检查后端是否启动成功
$backendReady = $false
for ($i = 1; $i -le 15; $i++) {
    try {
        $response = Invoke-WebRequest -Uri "http://localhost:5153/swagger" -Method GET -TimeoutSec 3 -ErrorAction SilentlyContinue
        if ($response.StatusCode -eq 200) {
            $backendReady = $true
            break
        }
    } catch {
        Write-Host "." -NoNewline -ForegroundColor Yellow
        Start-Sleep -Seconds 2
    }
}

if ($backendReady) {
    Write-Host ""
    Write-Host "✅ 后端启动成功! http://localhost:5153" -ForegroundColor Green
    Write-Host "📚 Swagger文档: http://localhost:5153/swagger" -ForegroundColor Cyan
} else {
    Write-Host ""
    Write-Host "❌ 后端启动检测超时，但可能仍在启动中..." -ForegroundColor Yellow
}

# 2. 启动前端  
Write-Host ""
Write-Host "🔧 启动前端服务..." -ForegroundColor Blue
Set-Location $frontendPath

# 检查依赖
if (-not (Test-Path "node_modules")) {
    Write-Host "📦 安装前端依赖..." -ForegroundColor Yellow
    if (Get-Command pnpm -ErrorAction SilentlyContinue) {
        pnpm install
    } else {
        npm install
    }
    
    if ($LASTEXITCODE -ne 0) {
        Write-Host "❌ 依赖安装失败" -ForegroundColor Red
        exit 1
    }
    Write-Host "✅ 依赖安装成功" -ForegroundColor Green
}

# 后台启动前端
Write-Host "🚀 启动前端开发服务器 (后台运行)..." -ForegroundColor Blue
$frontendJob = Start-Job -ScriptBlock {
    Set-Location "d:\2025\JGSY.PP\Project\JGSY.CMS.LowCode.Platform\wwwroot\frontend"
    if (Get-Command pnpm -ErrorAction SilentlyContinue) {
        pnpm start
    } else {
        npm start  
    }
}

# 等待前端启动
Write-Host "⏳ 等待前端启动..." -ForegroundColor Yellow
Start-Sleep -Seconds 15

# 检查前端是否启动成功
$frontendReady = $false
for ($i = 1; $i -le 20; $i++) {
    try {
        $response = Invoke-WebRequest -Uri "http://localhost:3000" -Method GET -TimeoutSec 3 -ErrorAction SilentlyContinue
        if ($response.StatusCode -eq 200) {
            $frontendReady = $true
            break
        }
    } catch {
        Write-Host "." -NoNewline -ForegroundColor Yellow
        Start-Sleep -Seconds 3
    }
}

Write-Host ""
if ($frontendReady) {
    Write-Host "✅ 前端启动成功! http://localhost:3000" -ForegroundColor Green
} else {
    Write-Host "❌ 前端启动检测超时，但可能仍在启动中..." -ForegroundColor Yellow
    Write-Host "请手动检查: http://localhost:3000" -ForegroundColor Cyan
}

# 显示最终结果
Write-Host ""
Write-Host "🎉 启动完成!" -ForegroundColor Green
Write-Host ""
Write-Host "📊 服务地址:" -ForegroundColor Blue  
Write-Host "  🌐 后端API: http://localhost:5153" -ForegroundColor Cyan
Write-Host "  📚 API文档: http://localhost:5153/swagger" -ForegroundColor Cyan
Write-Host "  🎨 前端应用: http://localhost:3000" -ForegroundColor Cyan

Write-Host ""
Write-Host "🧪 测试API:" -ForegroundColor Magenta
Write-Host "  📍 POST /api/auth/register - 用户注册"
Write-Host "  📍 POST /api/auth/login - 用户登录"  
Write-Host "  📍 GET /api/ConfigurationMigration/status - 配置状态"

Write-Host ""
Write-Host "💡 作业状态:" -ForegroundColor Yellow
Write-Host "  后端作业ID: $($backendJob.Id)"
Write-Host "  前端作业ID: $($frontendJob.Id)"
Write-Host ""
Write-Host "🛑 要停止服务，请运行:"  -ForegroundColor Red
Write-Host "  Stop-Job $($backendJob.Id), $($frontendJob.Id)"
Write-Host "  Remove-Job $($backendJob.Id), $($frontendJob.Id)"

Write-Host ""
Write-Host "✅ 应用已在后台运行！" -ForegroundColor Green
